package xonmp.notepaddemo;

import xonmp.notepaddemo.database.NotepadDbAdapter;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity {
	
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;
	
	private static final int INSERT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1;
	
	private NotepadDbAdapter mDbAdapter;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_list);
        mDbAdapter = new NotepadDbAdapter(this);
        mDbAdapter.open();
        fillData();
        registerForContextMenu(getListView());
    }
    
    private void fillData()
    {
    	Cursor notesCursor = mDbAdapter.fetchAllNotes();
    	startManagingCursor(notesCursor);
    	
    	// Create an array to specified the fields we want to 
    	// display in the list (only Title)
    	String[] from = new String[] {NotepadDbAdapter.KEY_TITLE};
    	
    	// and an array of the fields we want to bind those fields to
    	// (in this case just text1)
    	int[] to = new int[] {R.id.text1};
    	
    	// Now create a simple cursor adapter and set it
    	// to display
    	SimpleCursorAdapter notes =
    			new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
    	setListAdapter(notes);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	super.onCreateOptionsMenu(menu);
    	menu.add(0, INSERT_ID, 0, R.string.menu_insert);
    	return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item)
    {
    	switch(item.getItemId())
    	{
    	case INSERT_ID:
    		createNote();
    		return true;
    	}
    	
    	return super.onMenuItemSelected(featureId, item);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    {
    	super.onCreateContextMenu(menu, v, menuInfo);
    	menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
    	switch(item.getItemId())
    	{
    	case DELETE_ID:
    		AdapterContextMenuInfo info =
    			(AdapterContextMenuInfo) item.getMenuInfo();
    		mDbAdapter.deleteNote(info.id);
    		fillData();
    		return true;
    	}
    	return super.onContextItemSelected(item);
    }
    
    private void createNote()
    {
    	Intent i = new Intent(this, NoteEdit.class);
    	startActivityForResult(i, ACTIVITY_CREATE);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
    	super.onListItemClick(l, v, position, id);
    	Intent i = new Intent(this, NoteEdit.class);
    	i.putExtra(NotepadDbAdapter.KEY_ROWID, id);
    	startActivityForResult(i, ACTIVITY_EDIT);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
    	super.onActivityResult(requestCode, resultCode, intent);
    	fillData();
    }
}